//
//  DDMyQuestionDetailCollectionViewCell.m
//  dingdang
//
//  Created by Chen Jing on 15-7-26.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDMyQuestionDetailCollectionViewCell.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#import "DateUtil.h"


#import "DDMediaIV.h"
@interface DDMyQuestionDetailCollectionViewCell (){
    NSMutableParagraphStyle * paragraphStyle;
    NSMutableArray * array_record;
    NSRange timeRange;
}

@property (weak, nonatomic) IBOutlet UIImageView *imageView_star1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView_star2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView_star3;
@property (weak, nonatomic) IBOutlet UIImageView *imageView_star4;
@property (weak, nonatomic) IBOutlet UIImageView *imageView_star5;
@property (weak, nonatomic) IBOutlet UIImageView *imageView_adopt;



@end

@implementation DDMyQuestionDetailCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
    self.imageView_head.layer.cornerRadius = self.imageView_head.frame.size.width / 2.0;
    self.imageView_head.layer.masksToBounds = YES;

    paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 1;
    timeRange.location = 0;
    timeRange.length = 10;
    CGFloat btnWidth = CGRectGetWidth([[UIScreen mainScreen] bounds]) / 3.0;
    self.btn_bottom_rating.frame = CGRectMake(0, 0, btnWidth, 29);
    self.btn_bottom_adopt.frame = CGRectMake(0, btnWidth, btnWidth, 29);
    self.btn_bottom_followAsk.frame = CGRectMake(0, 2 * btnWidth, btnWidth, 29);
}


- (IBAction)btnClick_followAnswer:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(followAnswerWithCell:)]) {
        [self.delegate performSelector:@selector(followAnswerWithCell:) withObject:self];
    }
}
- (IBAction)btnClick_adopt:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(adoptWithCell:)]) {
        [self.delegate performSelector:@selector(adoptWithCell:) withObject:self];
    }
}

- (IBAction)btnClick_starRating:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(starRateWithCell:)]) {
        [self.delegate performSelector:@selector(starRateWithCell:) withObject:self];
    }
}
- (IBAction)btnClick_openOrCloseFollowAnswer:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(openOrCloseFollowAsk:)]) {
        [self.delegate performSelector:@selector(openOrCloseFollowAsk:) withObject:self];
    }
}

-(void)fixCell{
    if (self.info_starRating == 1) {
        self.imageView_star1.hidden = NO;
        self.imageView_star2.hidden = YES;
        self.imageView_star3.hidden = YES;
        self.imageView_star4.hidden = YES;
        self.imageView_star5.hidden = YES;
    }else if (self.info_starRating == 2){
        self.imageView_star1.hidden = NO;
        self.imageView_star2.hidden = NO;
        self.imageView_star3.hidden = YES;
        self.imageView_star4.hidden = YES;
        self.imageView_star5.hidden = YES;
    }else if (self.info_starRating == 3){
        self.imageView_star1.hidden = NO;
        self.imageView_star2.hidden = NO;
        self.imageView_star3.hidden = NO;
        self.imageView_star4.hidden = YES;
        self.imageView_star5.hidden = YES;
    }else if (self.info_starRating == 4){
        self.imageView_star1.hidden = NO;
        self.imageView_star2.hidden = NO;
        self.imageView_star3.hidden = NO;
        self.imageView_star4.hidden = NO;
        self.imageView_star5.hidden = YES;
    }else if (self.info_starRating == 5){
        self.imageView_star1.hidden = NO;
        self.imageView_star2.hidden = NO;
        self.imageView_star3.hidden = NO;
        self.imageView_star4.hidden = NO;
        self.imageView_star5.hidden = NO;
    }else{
        self.imageView_star1.hidden = YES;
        self.imageView_star2.hidden = YES;
        self.imageView_star3.hidden = YES;
        self.imageView_star4.hidden = YES;
        self.imageView_star5.hidden = YES;
    }

    if (self.info_adopt) {
        if (self.info_adopt.boolValue == YES) {
            self.imageView_adopt.hidden = NO;
        }else{
            self.imageView_adopt.hidden = YES;
        }
    }else{
        self.imageView_adopt.hidden = YES;
    }

    CGFloat tempInt = 1.0;
    if (self.btn_bottom_adopt.hidden == NO) {
        tempInt += 1.0;
    }
    if (self.btn_bottom_rating.hidden == NO) {
        tempInt += 1.0;
    }

    CGFloat tempWidth = CGRectGetWidth([[UIScreen mainScreen]bounds]) / tempInt;
    if (tempInt == 1) {
        self.btn_bottom_followAsk.frame =CGRectMake(0, 0, tempWidth, 29);
    }else if (tempInt == 2){
        self.btn_bottom_followAsk.frame = CGRectMake(tempWidth, 0, tempWidth, 29);
        if (self.btn_bottom_adopt.hidden == NO) {
            self.btn_bottom_adopt.frame = CGRectMake(0, 0, tempWidth, 29);
        }else{
            self.btn_bottom_rating.frame = CGRectMake(0, 0, tempWidth, 29);
        }
    }else{
        self.btn_bottom_rating.frame = CGRectMake(0, 0, tempWidth, 29);
        self.btn_bottom_adopt.frame = CGRectMake(tempWidth, 0, tempWidth, 29);
        self.btn_bottom_followAsk.frame = CGRectMake(tempWidth * 2, 0, tempWidth, 29);
    }

    CGRect body_tempRect = [self.label_body.text boundingRectWithSize:CGSizeMake(CGRectGetWidth([[UIScreen mainScreen] bounds]) - 16, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : self.label_body.font,NSParagraphStyleAttributeName : paragraphStyle} context:nil];
    
    float body_addHeight = body_tempRect.size.height > 20 ? body_tempRect.size.height - 20 : 20;
    
   
    if (self.isOpenFollowAsk == YES) {

        float totalHeight = 0;//body_addHeight;
        
        //ruanch bug:去除上次的覆盖视图
        for (UIView *view in self.contentOfFollowAsk.subviews) {
            [view removeFromSuperview];
        }
        

        if (self.info_followAskList) {
            for (int i = 0; i < self.info_followAskList.list.count; i++) {

                
                
                float holdOriginY = totalHeight;
                float tempAdd = holdOriginY;

                DDQuestionAnswerReplyCell * tempModel = self.info_followAskList.list[i];

                UIView * tempView = [[UIView alloc]initWithFrame:CGRectMake(0, totalHeight, CGRectGetWidth([[UIScreen mainScreen] bounds]), 0)];
               // tempView.backgroundColor = [UIColor colorWithRed:rand()*255/255.0 green:rand()*255/255.0 blue:rand()*255/255.0 alpha:1.0];//[UIColor clearColor];
                tempView.backgroundColor = [UIColor clearColor];
                
                [self.contentOfFollowAsk addSubview:tempView];

                //head
                UIImageView * imageView_head = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 45, 45)];
                imageView_head.layer.cornerRadius = 45 / 2.0;
                imageView_head.layer.masksToBounds = YES;
                [imageView_head sd_setImageWithURL:[NSURL URLWithString:tempModel.userAvatarUrl] placeholderImage:[UIImage imageNamed:@"个人中心-人数统计头像"]];
                [tempView addSubview:imageView_head];

                //name
                UILabel * label_name = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, 200, 20)];
                label_name.font = [UIFont systemFontOfSize:17];
                label_name.textColor = [UIColor colorWithRed:33/255.0 green:172/255.0 blue:229/255.0 alpha:1];
                label_name.text = tempModel.nickName;
                [tempView addSubview:label_name];

                //date
                UILabel * label_date = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 200, 20)];
                label_date.font = [UIFont systemFontOfSize:12];
                label_date.textColor = [UIColor lightGrayColor];
                label_date.text = [DateUtil format:@"YYYY-MM-dd HH:mm:ss" date:[NSDate dateWithTimeIntervalSince1970:[[tempModel.createTime substringWithRange:timeRange] integerValue]]];
                [tempView addSubview:label_date];

                UIImageView * flagImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth([[UIScreen mainScreen] bounds]) - 80, 10, 60, 20)];
                flagImageView.image = [UIImage imageNamed:@"题目组背景"];
                [tempView addSubview:flagImageView];

                UILabel * flagLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth([[UIScreen mainScreen] bounds]) - 80, 10, 55, 20)];
                flagLabel.textColor = [UIColor whiteColor];
                flagLabel.textAlignment = NSTextAlignmentRight;
                flagLabel.font = [UIFont systemFontOfSize:14];
                if (tempModel.children.count == 0 && [tempModel.parent integerValue] > 0) {
                    flagLabel.text = @"追答";
                }else{
                    flagLabel.text = @"追问";
                }
                
                [tempView addSubview:flagLabel];

                UIImageView * lineImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]), 1)];
                lineImage.image = [UIImage imageNamed:@"通用列表间隔线"];
                [tempView addSubview:lineImage];

                if (tempAdd > 0) {
                    totalHeight = label_date.frame.origin.y + label_date.frame.size.height + tempAdd;
                    tempAdd = 0;
                }else{
                    totalHeight = label_date.frame.origin.y + label_date.frame.size.height;
                }

                float addHeight = 0.0;
                for (DDQuestionAnswerReplyContent * content in tempModel.content) {
                    //body
                    if ([content.type isEqualToString:@"TEXT"]) {
                        CGRect tempRect = [content.text boundingRectWithSize:CGSizeMake(CGRectGetWidth([[UIScreen mainScreen] bounds]) - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14],NSParagraphStyleAttributeName : paragraphStyle} context:nil];
                        addHeight = tempRect.size.height;
                        UILabel * label_body = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, CGRectGetWidth([[UIScreen mainScreen] bounds]) - 20, tempRect.size.height)];
                        label_body.font = [UIFont systemFontOfSize:14];
                        label_body.numberOfLines = 0;
                        
                        label_body.text = content.text;
                        [tempView addSubview:label_body];

                        if (tempAdd > 0) {
                            totalHeight = label_body.frame.origin.y + label_body.frame.size.height + tempAdd;
                            tempAdd = 0;
                        }else{
                            totalHeight = label_body.frame.origin.y + label_body.frame.size.height;
                        }
                    }else if ([content.type isEqualToString:@"IMAGE"]) {//ruanch bug:如果没有文字的情况，将累加上次的结果
                        totalHeight = 50;
                    }else{
                        totalHeight = 0;
                    }
                }


             //image and record
                for (int j = 0; j < tempModel.content.count;j++) {
                    DDQuestionAnswerReplyContent * content = tempModel.content[j];
                    if ([content.type isEqualToString:@"IMAGE"]) {
                        //UIImageView * imageView_body = [[UIImageView alloc]initWithFrame:CGRectMake(10, 60 + addHeight , CGRectGetWidth([[UIScreen mainScreen] bounds]) - 20, 200)];
                        //[imageView_body sd_setImageWithURL:[NSURL URLWithString:content.url] placeholderImage:[UIImage imageNamed:@"首页-答疑-客观题-单选习题拍照图-通用"]];
                        //[tempView addSubview:imageView_body];
                        //增加标签
                        [self addTag:tempView model:content postion:addHeight];
                        addHeight += 210;
                        totalHeight += 210;
                    }else if ([content.type isEqualToString:@"VOICE"]){
                        if (array_record == nil) {
                            array_record = [NSMutableArray array];
                        }

                        [array_record addObject:content.url];

                        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(10, 90 + addHeight, 60, 30)];
                        [btn setBackgroundImage:[UIImage imageNamed:@"bubble_stroked"] forState:UIControlStateNormal];
                        btn.tag = array_record.count;
                        [btn addTarget:self action:@selector(playRecord:) forControlEvents:UIControlEventTouchUpInside];
                        UIImageView * imageRecord = [[UIImageView alloc]initWithFrame:CGRectMake(35, 5, 20, 20)];
                        [btn addSubview:imageRecord];
                        
                        [tempView addSubview:imageRecord];
                        addHeight += 40;
                        totalHeight += 40;
                    }
                }

                totalHeight += 10;
                totalHeight += holdOriginY;

                if (holdOriginY == 0) {
                    tempView.frame = CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]), totalHeight );
                }else{
                    tempView.frame = CGRectMake(0, holdOriginY, CGRectGetWidth([[UIScreen mainScreen] bounds]), totalHeight - holdOriginY);

                }

                lineImage.frame = CGRectMake(0, tempView.frame.size.height - 1, CGRectGetWidth([[UIScreen mainScreen] bounds]), 1);

            }

        }

        self.contentOfFollowAsk.frame = CGRectMake(0, self.contentOfBottom.frame.origin.y + self.contentOfBottom.frame.size.height, CGRectGetWidth([[UIScreen mainScreen] bounds]), totalHeight);
    }else{
        for (UIView * tempSubView in self.contentOfFollowAsk.subviews) {
            [tempSubView removeFromSuperview];
        }
    }
}

-(void)addTag:(UIView *)tempView model:(DDQuestionAnswerReplyContent *)content postion:(NSInteger)addHeight
{
    //标签 ruanch-增加
    NSURL *url = [NSURL URLWithString:content.url];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
    DDMediaIV *imageView_body = [[DDMediaIV alloc] initWithFrame:CGRectMake(10, 60 + addHeight , CGRectGetWidth([[UIScreen mainScreen] bounds]) - 20, 200)];
    imageView_body.userInteractionEnabled = YES;
    imageView_body.isOnlyRead = YES;
    imageView_body.url = content.url;
    [imageView_body sd_setImageWithURL:[NSURL URLWithString:content.url] placeholderImage:[UIImage imageNamed:@"首页-答疑-客观题-单选习题拍照图-通用"]options:SDWebImageRefreshCached];
    for (DDQuestionAnswerReplyContentLabel *labelInfo in content.labels) {
        double c_x = ([labelInfo.x integerValue]/image.size.width)*(tempView.frame.size.width - 20);
        double c_y = ([labelInfo.y integerValue]/image.size.height)*200;
        
        [imageView_body addMedia:labelInfo.text type:labelInfo.type x:c_x y:c_y url:labelInfo.url];
    }
    //标签 ruanch end
    [tempView addSubview:imageView_body];
}


-(void)playRecord:(UIButton *)btn{
    if (btn.tag < array_record.count) {

    }
}

@end
